package com.nulldev.util.web.WebSocketAPI;

public class WebSocketMessage {
	private byte[] byteMessage;
	private String stringMessage;
	private byte opcode;

	public WebSocketMessage(byte[] message) {
		this.byteMessage = message;
		this.opcode = WebSocket.OPCODE_BINARY;
	}

	public WebSocketMessage(String message) {
		this.stringMessage = message;
		this.opcode = WebSocket.OPCODE_TEXT;
	}

	public boolean isText() {
		return opcode == WebSocket.OPCODE_TEXT;
	}

	public boolean isBinary() {
		return opcode == WebSocket.OPCODE_BINARY;
	}

	public byte[] getBytes() {
		return byteMessage;
	}

	public String getText() {
		return stringMessage;
	}

	@Override
	public String toString() {
		String type_s = "N/A";
		if (isText()) {
			type_s = "TEXT";
		} else if (isBinary()) {
			type_s = "BINARY";
		}
		Object b = isText() ? getText() : this.getBytes();
		return "WebSocketMessage[type=" + type_s + ",content=" + b + "]";
	}

	public int type() {
		return isText() ? 1 : 0;
	}
}
